import React from 'react'
import Sun from './Sun'
import { useDispatch, useSelector } from 'react-redux/es/exports'
import { setName } from './store/createAction'
import axios from 'axios'

const getRes = async (dispatch) => {
  const res = await axios.get('https://www.escook.cn/api/cart')
  // console.log(res)
  dispatch(setName(res.data.message)) // 真正修改状态
}
export default function Child() {
  const username = useSelector((state) => state.username)

  //得到dispatch方法 修改数据
  const dispatch = useDispatch()

  const request = () => {
    // dispatch(async () => {
    //   const res = await axios.get('https://www.escook.cn/api/cart')
    //   console.log(res)
    //   dispatch(setName(res.data.message))
    // })

    dispatch(getRes)
  }
  return (
    <div>
      <h5>Child-{username}</h5>
      <button
        onClick={() => {
          dispatch(setName('教的什么东西'))
        }}
      >
        改名字
      </button>
      <button onClick={request}>发送请求</button>
      <Sun></Sun>
    </div>
  )
}
